package cas.ihep.fs.impl;

import cas.ihep.fs.UnifiedFileSystem;
import org.apache.hadoop.conf.Configuration;

import java.util.HashMap;

public class UnifiedConf extends HashMap<String,Object> implements UnifiedFileSystem.Configuration{

    private UnifiedConf(){

    }

    public static UnifiedConf newConf(org.apache.hadoop.conf.Configuration eConf){
        UnifiedConf ret=new UnifiedConf();
        if(eConf==null){
            eConf=new org.apache.hadoop.conf.Configuration();
        }
        ret.hdpConf=eConf;
        return ret;
    }

    public UnifiedConf(org.apache.hadoop.conf.Configuration eConf){
        if(eConf==null){
            eConf=new org.apache.hadoop.conf.Configuration ();
        }
        hdpConf=new org.apache.hadoop.conf.Configuration(eConf);
    }

    @Override
    public int getInt(String key) {
        Object ret=get(key);
        if(ret!=null && ret instanceof Integer){
            return (Integer)ret;
        }
        String hdp;
        if((hdp=hdpConf.getTrimmed(key))!=null){
            try {
                return Integer.parseInt(hdp);
            }catch (NumberFormatException ignored){}
        }
        if((hdp=System.getProperty(key))!=null){
            return Integer.parseInt(hdp.trim());
        }
        throw new RuntimeException("No such element");
    }

    @Override
    public String getString(String key) {
        Object ret=get(key);
        if(ret!=null && ret instanceof String){
            return (String)ret;
        }
        String hdp;
        if((hdp=hdpConf.get(key))!=null){
            return hdp;
        }
        if((hdp=System.getProperty(key))!=null){
            return hdp;
        }
        throw new RuntimeException("No such element");
    }

    @Override
    public boolean getBoolean(String key) {
        Object ret=get(key);
        if(ret!=null && ret instanceof Boolean){
            return (Boolean)ret;
        }
        String hdp;
        if((hdp=hdpConf.getTrimmed(key))!=null){
            return Boolean.parseBoolean(hdp);
        }
        if((hdp=System.getProperty(key))!=null){
            return Boolean.parseBoolean(hdp.trim());
        }
        throw new RuntimeException("No such element");
    }

    @Override
    public int getInt(String key, int defVal) {
        Object ret=get(key);
        if(ret!=null && ret instanceof Integer){
            return (Integer)ret;
        }
        String hdp;
        if((hdp=hdpConf.getTrimmed(key))!=null){
            try{
                return Integer.parseInt(hdp);
            }catch (NumberFormatException ignored){}
        }
        if((hdp=System.getProperty(key))!=null){
            try{
                return Integer.parseInt(hdp.trim());
            }catch (NumberFormatException ignored){}
        }
        return defVal;
    }

    @Override
    public String getString(String key, String defVal) {
        Object ret=get(key);
        if(ret!=null && ret instanceof String){
            return (String)ret;
        }
        String hdp;
        if((hdp=hdpConf.get(key))!=null){
            return hdp;
        }
        if((hdp=System.getProperty(key))!=null){
            return hdp;
        }
        return defVal;
    }

    @Override
    public boolean getBoolean(String key, boolean defVal) {
        Object ret=get(key);
        if(ret!=null && ret instanceof Boolean){
            return (Boolean)ret;
        }
        String hdp;
        if((hdp=hdpConf.getTrimmed(key))!=null){
            return Boolean.parseBoolean(hdp);
        }
        if((hdp=System.getProperty(key))!=null){
            return Boolean.parseBoolean(hdp.trim());
        }
        return defVal;
    }

    @Override
    public Configuration hadoopConf() {
        return hdpConf;
    }

    private org.apache.hadoop.conf.Configuration hdpConf;
}
